/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package database;

/**
 *
 * @author Alejandro
 */
public class DatabaseFloat extends DatabaseObject {
    public final static String PRECISE = "precise";
    public final static String SCALE = "scale";
    private int precise = 7;
    private int scale = 2;
    private double lowerBound = Double.MIN_VALUE;
    private double upperBound = Double.MAX_VALUE;

    public DatabaseFloat() {
        value = 0.0;
    }

    @Override
    public void setValue(Object value) {
        try {
            this.value = (Object) Double.parseDouble((String) value);
        } catch (NumberFormatException nfe) {
            this.value = null;
        }
    }

    @Override
    public void setParam(String param, Object value) {
        if (param.equals(DatabaseNumber.LOWER_BOUND)) {
            lowerBound = Double.parseDouble((String) value);
        }
        if (param.equals(DatabaseNumber.UPPER_BOUND)) {
            upperBound = Double.parseDouble((String) value);
        }
        if (param.equals(PRECISE)) {
            precise = Integer.parseInt((String) value);
        }
        if (param.equals(SCALE)) {
            scale = Integer.parseInt((String) value);
        }
    }

    @Override
    public Object getParam(String param) {
        if (param.equals(DatabaseNumber.LOWER_BOUND)) {
            return (Object) lowerBound;
        }
        if (param.equals(DatabaseNumber.UPPER_BOUND)) {
            return (Object) upperBound;
        }
        if (param.equals(PRECISE)) {
            return (Object) precise;
        }
        if (param.equals(SCALE)) {
            return (Object) scale;
        }
        return null;
    }

    @Override
    public String toString() {
        /*
        DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance();
        df.setMinimumFractionDigits(scale);
        df.setMaximumFractionDigits(scale);
        df.setMinimumIntegerDigits(1);
        df.setMaximumIntegerDigits(precise-scale);
        df.setGroupingSize(3);
        return df.format(value);
        */
        return Double.toString((Double) value);
    }

    @Override
    protected void acceptValidator(Validator v) {
        v.validateFloat(this);
    }

    @Override
    public int compareTo(DatabaseObject o) {
        Double d1 = (Double) value;
        Double d2 = (Double) o.getValue();
        return d1.compareTo(d2);
    }

}
